<?php
/* 
 */
class Curl
{
	static $config = array();
	function login(){
		$ch = curl_init($this->config['login_data']['url']);
		//print_r($this->config);exit;
		//ssl 登陆
		if($this->config['login_data']['ssl']){
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_REFERER, $this->config['login_data']['referer']);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->config['cookie_jar']);
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $this->config['cookie_jar']);
		
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $this->config['login_data']['data']);
		$res = curl_exec($ch);
		if(curl_errno($ch))	exit(curl_error($ch)); 
		curl_close($ch);
		exit($res);
	}
	
	function get($url, $match = null, $charset = 'utf-8'){
		//exit($match);
		$ch = curl_init($url);
		if($this->config['login_data']['ssl']){
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
		}
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->config['header']);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->config['useragent']);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->config['cookie_jar']);
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $this->config['cookie_jar']);
		curl_setopt($ch, CURLOPT_REFERER, $this->config['referer']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//是否抓取跳转后的内容
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		$res = curl_exec($ch);
		if(curl_errno($ch))	exit(curl_error($ch)); 
		curl_close($ch);
		if($charset != 'utf-8'){
			$res = convert_str($res, $charset, 'utf-8');
		}
		//echo $res;exit;
		if($match){
			//echo $res;
			//echo $match;exit;
			//echo $url;exit;
			preg_match('/'.$match.'/is',$res,$r);
			$res = $r[1] ?  $r[1] : $res;
		}
		curl_log('get',$res);
		return $res;
	}
	
	function post($data, $match, $charset = 'utf-8'){

		/* foreach($data as $key => &$val){
			$val = urlencode($val);
		} */
		$data = $data ? $data : array();
		$data = array_merge($this->config['post_data'], $data);

		//print_test($data);
		$ch = curl_init();
		if($this->config['login_data']['ssl']){
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
		}
		//exit($data['referer']);
		curl_setopt($ch, CURLOPT_URL, $data['url']);
		curl_setopt($ch, CURLOPT_REFERER, $data['referer']);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->config['header']);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->config['useragent']); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->config['cookie_jar']);
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $this->config['cookie_jar']);
		curl_setopt($ch, CURLOPT_POST,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data['data']);
		//是否抓取跳转后的内容
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		$res = curl_exec($ch);
		if(curl_errno($ch))	exit(curl_error($ch)); 
		curl_close($ch);
		if($charset != 'utf-8'){
			$res = convert_str($res, $charset, 'utf-8');
		}
		//print_r($this->config);exit;
		//exit($res);
		if(isset($this->config['code'])){
			preg_match('/\"code\":\"(.+?)\"/is',$res,$code);
			$str = $this->config['code'][$code[1]];
			$res = $str ? $str : $code[1];
		}
		curl_log('post',$res);
		return $res;
	}
	
	function __construct($type, $account, $login = true){
		$this->type = $type;
		switch ($type){
			
			case 'yahoo':
				
				$username = rawurlencode($account['username']);
				$password = rawurlencode($account['password']);
				
				$this->config = array(
					'account' => array(
						'username' => $username,
						'password' => $password,
					),
					'login_data' => array(
						'ssl' => true,
					),
					'charset' => 'euc-jp',
					'post_data' => array(
					),
					'useragent' => "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
					'referer' => "http://auctions.yahoo.co.jp/",
					'header' => array(
						'Accept-Language: zh-cn,zh;q=0.5',
						'Connection: Keep-Alive',
						'Cache-Control: no-cache',
						'Host: login.yahoo.co.jp',
						'referer: https://login.yahoo.co.jp/config/login?.src=auc&.pd=&.done=http%3A//auctions.yahoo.co.jp/',
						
					),
				);
			
			break;
			
			
			case 'letian':
				
				$username = rawurlencode($account['username']);
				$password = rawurlencode($account['password']);
				
				$this->config = array(
					'account' => array(
						'username' => $username,
						'password' => $password,
					),
					'login_data' => array(
						'url' => 'https://grp02.id.rakuten.co.jp/rms/nid/login',
						'referer' => 'https://grp02.id.rakuten.co.jp/rms/nid/vc?service_id=a15&__a=-2104224813&__event=login',
						'data' => "__a=-2104224813&return_url=rms%2Fmya%2F&service_id=a15&u={$username}&p={$password}&submit=%A5%ED%A5%B0%A5%A4%A5%F3&auto_logout=true",
						'ssl' => true,
					),
					'charset' => 'euc-jp',
					'post_data' => array(
					),
					'useragent' => "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
					'referer' => "https://www.rakuten.co.jp",
					'header' => array(
						'Accept-Language: zh-cn,zh;q=0.5',
						'Connection: Keep-Alive',
						'Cache-Control: no-cache',
						'Host: my.auction.rakuten.co.jp',
					),
				);
			
			break;
			
		}

		
		$cookie_jar = realpath('./data/')."/cookie_{$this->type}.txt";
		$this->config['cookie_jar'] = $cookie_jar;

		//创建cookie文件
		if(!file_exists($this->config['cookie_jar'])){
			$handle = fopen($this->config['cookie_jar'] , "w");
		    fclose($handle);
		}

		//获取cookie最后修改时间
		$cookie_time = filemtime($this->config['cookie_jar']);	

		//需要登陆且未登陆过
		if($login == true || (time() - $cookie_time) >= 8*3600){
			$this->login();
		}
		
		
	}
}